/**
 * Created by trisatria on 2/21/14.
 */
$(document).on('pageshow','#editproduct', function(event) {

    $.mobile.loading( "show");
    var product_id = getUrlVars();
    product_id = product_id['product_id'];
    $.ajax({
        url: _host+'topic/detail/'+product_id
    }).done(function(data){
            j_data = $.parseJSON(data);

            var j_topic = j_data['topic'];
        console.log(j_topic);
            var j_gel = j_data['img'];
            j_topic = $.parseJSON(j_topic);
            j_gel = $.parseJSON(j_gel);


            $('#user_id').val(user_id);
            $('#product_id').val(product_id);
            $('#title').val(j_topic['title']);
            $('#price').val(j_topic['price']);
            $('#date').val(j_topic['expire_date']);
            $('#short_description').text(j_topic['short_description']);
            $('#full_description').text(j_topic['full_description']);
            $('#images').val(j_topic['images']);

            value = j_topic['is_brandnew'];
            $("input[name=is_brandnew][value=" + value + "]").attr('checked', 'checked');
            $("input[name=is_brandnew][value=" + value + "]").attr('data-cacheval', true);
            $("input[name=is_brandnew][value=" + value + "]").parent().children('label').addClass('ui-radio-on');

            value = j_topic['show_price'];
            $("input[name=show_price][value=" + value + "]").attr('checked','checked');
            $("input[name=show_price][value=" + value + "]").attr('data-cacheval', false);
            $("input[name=show_price][value=" + value + "]").parent().children('label').addClass('ui-radio-on');


            value = j_topic['topic_type'];
            $("input[name=topic_type][value=" + value + "]").attr('checked','checked');
            $("input[name=topic_type][value=" + value + "]").attr('data-cacheval', false);
            $("input[name=topic_type][value=" + value + "]").parent().children('label').addClass('ui-radio-on');
            //$("input[name=topic_type][value=" + value + "]").addClass('ui-radio-on');


            //init location
            for(var i=0; i< _topic_loc.length ; i = parseInt(i) + 1){
                if(i == j_topic['topic_location']){

                    $('#topic_location').parent().children('span').text(_topic_loc[i]);
                    var content = '<option value="'+i+'" selected >'+_topic_loc[i]+'</option>';
                }
                else
                    var content = '<option value="'+i+'" >'+_topic_loc[i]+'</option>';
                $('#topic_location').append(content);

            }
            //init cat
            for(var i=0; i< _topic_cat.length ; i = parseInt(i) + 1){

                if( _topic_cat[i]['id']== j_topic['cat_id']){
                    $('#cat_id').parent().children('span').text( _topic_cat[i]['title']);
                    var content = '<option value="'+_topic_cat[i]['id']+'" selected >'+_topic_cat[i]['title']+'</option>';
                }
                else
                    var content = '<option value="'+_topic_cat[i]['id']+'" >'+_topic_cat[i]['title']+'</option>';
                $('#cat_id').append(content);

            }//
             $('#editproduct #price').formatCurrency($.formatCurrency.regions['vi-VN']);

            console.log(j_gel);
            //show media
            _class = "thumb_border";
            /*var img = '<div class="col50 '+_class+'">';
            img += '<img class="gel_item" style="float: left; width: 100%" src="'+_host_img +'/'+j_topic['images']+'">';
            img += '  <div class="img_control" data-role="">';
            img += '    <button data_rel="'+_host_img +'/'+ j_topic['images']+'" class="img_set_thumnail ui-btn ui-icon-star ui-btn-icon-left ui-shadow ui-corner-all">Hình Đại Diện</button>';
            img += ' <button class="img_del ui-btn ui-icon-delete ui-btn-icon-left ui-shadow ui-corner-all">Xoá</button>';
            img += '<button class="img_close ui-btn ui-icon-power ui-btn-icon-left ui-shadow ui-corner-all">đóng</button>';
            img += '</div>';
            img += '    </div>';
        $('#img_append').append(img)
            */
            for(var i = 0; i <  j_gel.length ; i= parseInt(i) + 1){

                //console.log(j_gel[i]);
                var _class="";
                if(j_gel[i]['link'] == j_topic['images'])
                    _class = "thumb_border";
                var img = '';
                img = '<div class="col50 '+_class+'">';
                img += '<img class="gel_item" style="float: left; width: 100%" src="'+_host_img +'/'+j_gel[i]['link']+'">';
                img += '  <div class="img_control" data-role="">';
                img += '    <button data_rel="'+_host_img +'/'+ j_gel[i]['link']+'" class="img_set_thumnail ui-btn ui-icon-star ui-btn-icon-left ui-shadow ui-corner-all">Hình Đại Diện</button>';
                img += ' <button class="img_del ui-btn ui-icon-delete ui-btn-icon-left ui-shadow ui-corner-all">Xoá</button>';
                img += '<button class="img_close ui-btn ui-icon-power ui-btn-icon-left ui-shadow ui-corner-all">đóng</button>';
                img += '</div>';
                img += '    </div>';
                $('#img_append').append(img);
            }
            $.mobile.loading( "hide");
        });

    $('#btn_edit_product').click(function(){
        var topic_user_id = $('#user_id').val();
        var id = $('#product_id').val();
        var title = $('#title').val();
        var price = $('#price').toNumber().val().replace('.','');
        var expire_date = $('#date').val();
        var images = $('.thumb_border img').attr('src');
        images = images.split('/');
        images = images[images.length - 1];

        var topic_location =  $('#topic_location').find(":selected").val();
        var cat_id =  $('#cat_id').find(":selected").val();
        var short_description = $('#short_description').val();
        var full_description = $('#full_description').val();
        var topic_type = $('input[name=topic_type]:checked').val();
        var show_price = $('input[name=show_price]:checked').val();
        var is_brandnew = $('input[name=is_brandnew]:checked').val();
        $.ajax({
            url : _host+"topic/add",
            type: "POST",
            data: {
                id: id,
                title: title,
                price: price,
                //expire_date: expire_date,
                topic_location: topic_location,
                cat_id: cat_id,
                short_description: short_description,
                full_description: full_description,
                topic_type: topic_type,
                show_price: show_price,
                is_brandnew: is_brandnew,
                user_id: user_id,
                images: '/upload/image/'+images,
                thumb:  '/upload/image/'+images
            }
        }).done(function(data){
                if(data != -1){
                    var item_array = new Array();
                    $( ".gel_item" ).each(function() {
                        //console.log($(this).attr('src'));
                        //alert($(this).parent().hasClass('thumb_border'));
                            var item_data = {};
                            item_data['topic_id'] = data;
                            item_data['user_id'] = user_id;
                            item_data['link'] = $(this).attr('src');
                            item_data['link'] = item_data['link'].replace(_host_img+'/','');
                            item_array.push(item_data);
                    });
                    console.log(JSON.stringify(item_array));
                    $.ajax({
                        url : _host+"topic/addgellary",
                        type: "POST",
                        data: {data: JSON.stringify(item_array) ,  isedit: '1', topic_id: data }
                    });
                    $.mobile.changePage('productdetail.html?product_id='+data, {  role: "page", reverse: true } );
                }
            });
    })

})